package Model;
import java.util.*;
import java.io.Serializable;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.io.FileInputStream;
import java.io.ObjectInputStream;

import javax.swing.DefaultListModel;

import java.awt.Image;
import java.util.Date;

//The class for the contact list

/**
 * this model is a list of contact with the methods to manipulate it and display info correctly
 */

public class ContactList implements Serializable {

	public static final String ALL = "ALL";
	public static final String NOTHING ="";	
	public static final String SPACE =" ";	
	public static final String CONTACTLIST_FILE = "contactList.serial";
	
	private static final long serialVersionUID = 1L;
	
	private ArrayList<Contact> contacts;
	
	public ContactList() {
		contacts = new ArrayList<Contact>();
	}
	
	public void addContact(Contact contact) {
		contacts.add(contact);
	}
	public void deleteContact(Contact contact) {
		contacts.remove(contact);
	}
	public ArrayList<Contact> getArray() {
		return contacts;
	}
	
	
	/**
	 * 
	 * @return the default list model for the view with all the names of the contacts
	 */
	
	public DefaultListModel<String> getListModel() {
		DefaultListModel<String> listModel = new DefaultListModel<String>();
		String Name = new String();
		StringBuilder srtBuild= new StringBuilder();
		
		for (int i=0;i<contacts.size(); i++) {
			srtBuild.setLength(0);
			srtBuild.append(contacts.get(i).getFirstName()).append(SPACE).append(contacts.get(i).getLastName());
			Name = srtBuild.toString();
			listModel.addElement(Name);
		}
		
		return listModel;
	}
	
	/**
	 * 
	 * @return the default list model for the view with all the names of the contacts sorted by group name
	 */
	
	
	public DefaultListModel<String> getListModelByGroup(String Group) {
		DefaultListModel<String> listModel = new DefaultListModel<String>();
		String Name = new String();
		StringBuilder srtBuild= new StringBuilder();
		
		if (Group.equals(ALL)){ //the "all" group displays all the contacts
				for (int i=0;i<contacts.size(); i++) {
					
					srtBuild.setLength(0);
					srtBuild.append(contacts.get(i).getFirstName()).append(SPACE).append(contacts.get(i).getLastName());
					Name = srtBuild.toString();
					listModel.addElement(Name);
			}
		}
		else {
			for (int i=0;i<contacts.size(); i++) {
				
				if (contacts.get(i).getGroup().equals(Group)) {
					
					srtBuild.setLength(0);
					srtBuild.append(contacts.get(i).getFirstName()).append(SPACE).append(contacts.get(i).getLastName());
					Name = srtBuild.toString();
					listModel.addElement(Name);
				
				}
			}
		}
		
		
		return listModel;
	}
	
	/**
	 * 
	 * @return the default list model for the view with all the names of the contacts filtered by a specific string
	 */
	
	public DefaultListModel<String> getListModelSearch(String nameSearched) {
		
		DefaultListModel<String> listModel = new DefaultListModel<String>();
		String Name = new String();
		StringBuilder srtBuild= new StringBuilder();
		
		if (nameSearched.equals(NOTHING)){ //displays all the contacts
				for (int i=0;i<contacts.size(); i++) {
					
					srtBuild.setLength(0);
					srtBuild.append(contacts.get(i).getFirstName()).append(SPACE).append(contacts.get(i).getLastName());
					Name = srtBuild.toString();
					listModel.addElement(Name);
			}
		}
		else {
			for (int i=0;i<contacts.size(); i++) {					
				
				srtBuild.setLength(0);
				srtBuild.append(contacts.get(i).getFirstName()).append(SPACE).append(contacts.get(i).getLastName());
				Name = srtBuild.toString();
				if (Name.toLowerCase().contains(nameSearched.toLowerCase()) ) {
					listModel.addElement(Name);
				}
				
				}
			}
		
		
		return listModel;
	}
	
		
	/**
	 * 
	 * @return the default list model for the view with all the names of the groups
	 */	
	
	
	public DefaultListModel<String> getGoupsListModel() {
		this.sortByGroup () ;
		
		DefaultListModel<String> listModel = new DefaultListModel<String>();
		String Group1 = new String();
		String Group2 = new String();
		listModel.addElement(ALL);
		
		for (int i=0;i<contacts.size(); i++) {
			if (i!=0)
			{
				
				Group1 = contacts.get(i-1).getGroup();
				Group2 = contacts.get(i).getGroup();
				if (!Group1.equals(Group2)) {
					listModel.addElement(Group2);
				}
				
			}
			else {
				Group1 = contacts.get(i).getGroup();
				listModel.addElement(Group1);
			}
		}
		
		return listModel; 
		
	}
	
	/**
	 * 
	 * sort the list by name
	 */	
	
	public void sortByName () {
		
		Collections.sort(contacts, new Comparator<Contact>() {
			
			public int compare(Contact a, Contact b) {

				if(a.getLastName().compareTo(b.getLastName())==0) {
					return a.getFirstName().compareTo(b.getFirstName());
				}
				else return a.getLastName().compareTo(b.getLastName());
			}
		});

	}
	
	public void sortByGroup () {
		
		Collections.sort(contacts, new Comparator<Contact>() {
			
			public int compare(Contact a, Contact b) {
				
				return a.getGroup().compareTo(b.getGroup());
			}
		});

	}
	
	public void sortByDate () {
		
		Collections.sort(contacts, new Comparator<Contact>() {
			
			public int compare(Contact a, Contact b) {
				
				return a.getDate().compareTo(b.getDate());
			}
		});

	}
	
	/**
	 * 
	 * return a contact identified by his first name and last name
	 */	
	
	public Contact findContract (String FirstName, String LastName) {
		Contact contact;
		
		for (int i=0;i<contacts.size(); i++) {
			contact = contacts.get(i);
			
			if (contact.getFirstName().equals(FirstName) && contact.getLastName().equals(LastName) ) {
				return contact;
			}
			

		}
		return null;
	}
	
	/**
	 * 
	 * save the contacts in a file
	 */	
	
	public void saveContacts (ContactList contactList){
		try {
			
			FileOutputStream fout = new FileOutputStream(CONTACTLIST_FILE);
			ObjectOutputStream oout = new ObjectOutputStream(fout);
			
			try {
				oout.writeObject(contactList);
				oout.flush();
				
			} finally {
				try {
					oout.close();
				} finally {
					fout.close();
				}
			}
		
		}catch(IOException ioe) {
			ioe.printStackTrace();
		}
	}
	
	
	/**
	 * 
	 * load the contacts from a file
	 */	
	
	public ContactList loadContacts(){
		ContactList contactList = new ContactList();
		
		try {
			
			FileInputStream fin = new FileInputStream (CONTACTLIST_FILE);
			ObjectInputStream oin = new ObjectInputStream (fin);
			try {
				contactList =(ContactList) oin.readObject();
			} finally {
				try {
					oin.close();
				}finally {
					fin.close();
				}
			}
			
		} catch (IOException e){
			e.printStackTrace();	
		}
		catch (ClassNotFoundException e2){
			e2.printStackTrace();
		}
		if (contactList!=null){
		}

		return contactList;
	}


	public String toString() {
		String info = new String();
		for (int i=0;i<contacts.size(); i++) {
			
			info += contacts.get(i).toString();
		}
		
		return info;
		
	}


	
	
	
}
